﻿using System.Collections.Generic;
using System.Linq;
using GC_FFW.BusinessEntities.Abstractions.Interfaces.Forecast;
using GC_FFW.BusinessEntities.Forecast.Xml;
using GC_FFW.ForecastWriter.Abstractions.Adapters;
using GC_FFW.ForecastWriter.Abstractions.Interfaces.Helpers;
using GC_FFW.ForecastWriter.Common;

namespace GC_FFW.ForecastWriter.Adapters.TeamMatch.ConfrontationsStatistics
{
	public class AboutWinnerProposalAdapter : ProposalBaseAdapter
	{
		#region Constructors

		public AboutWinnerProposalAdapter(IForecastData forecastData, Conditions conditions,
			IStringManager stringManager, IRandomizer randomizer)
			: base(forecastData, conditions, stringManager, randomizer) { }

		#endregion

		#region Overrides

		public override string GetText()
		{
			if (ForecastData.PersonalConfrontationStatistics == null) return string.Empty;

			var countMatches = ForecastData.PersonalConfrontationStatistics.Matches != null
					? ForecastData.PersonalConfrontationStatistics.Matches.Count()
					: 0;
			var hasWinnerTeamStatistics = ForecastData.PersonalConfrontationStatistics.HasWinnerTeamStatistics;

			if (countMatches > 0 && hasWinnerTeamStatistics)
			{
				var proposal = GetProposal(Conditions.GetCondition(1));
				if (proposal == null) return string.Empty;

				var result = proposal.Text ?? string.Empty;
				if (proposal.KeywordList == null || !proposal.KeywordList.Any()) return result;

				var dictionary = new Dictionary<string, string>();
				foreach (var keyword in proposal.KeywordList)
				{
					switch (keyword)
					{
						case Constants.WINNER_TEAM_STATISTICS_NAME_KEYWORD:

							dictionary.Add(
								Constants.WINNER_TEAM_STATISTICS_NAME_KEYWORD,
								ForecastData.PersonalConfrontationStatistics.WinnerTeamStatisticsName ?? string.Empty);

							break;
					}
				}

				result = StringManager.ReplaceKeywords(result, dictionary);

				return result;
			}
			if (countMatches > 0 && !hasWinnerTeamStatistics)
			{
				var proposal = GetProposal(Conditions.GetCondition(2));
				return proposal != null && proposal.Text != null ? proposal.Text : string.Empty;
			}
			else
			{
				var proposal = GetProposal(Conditions.GetCondition(3));
				return proposal != null && proposal.Text != null ? proposal.Text : string.Empty;
			}
		}

		#endregion
	}
}
